<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>layui</title>
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

	<link rel="stylesheet" href="../../../static/font-awesome-4.7.0/css/font-awesome.min.css">
	<link rel="stylesheet" href="../../../static/vui/css/vui.css">

	<script type="text/javascript" src="../../../static/js/jquery-1.9.1.min.js"></script>
	<script type="text/javascript" src="../../../static/js/jquery.cookie.min.js"></script>
	<script type="text/javascript" src="../../../static/layer/layer.js"></script>
	<script type="text/javascript" src="../../../static/laydate/laydate.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/vui.js"></script>
	<script type="text/javascript" src="../../../static/vui/js/datagrid.js"></script>
	<script type="text/javascript" src="../../../static/js/common.js"></script>
</head>
<body>

<div id="formArea">
	<table class="table-top">
		<tr>
			<td>
				<label>供应商</label>
			</td>
			<td>
				<div id="relationId"></div>
			</td>
			<td>
				<label>单据编号</label>
			</td>
			<td>
				<input type="text" class="input input-query" name="code" autocomplete="off"/>
			</td>
			<td>
				<label class="label">单据日期</label>
			</td>
			<td>
				<input type="text" n="date" class="input search-date" name="dateStart" autocomplete="off"/>&nbsp;-&nbsp;
				<input type="text" n="date" class="input search-date" name="dateEnd" autocomplete="off"/>
			</td>
			<td>
				<label>单据状态</label>
			</td>
			<td>
				<div id="statusId"></div>
			</td>
			<td>
				<button class="button" id="query"><span>查询</span></button>
				<button class="button" id="create"><span>新增</span></button>
			</td>
		</tr>
	</table>
</div>
</fieldset>
<div id="table"></div>

<script>
	var orderType = 3;
    $(function(){
        $('#create').click(function(){
            location.href = 'buy_add.html';
        });

        $('#relationId').search({
            name:'relationId',
            click:function () {
                app.window(layer,{
                    title:'供应商选择',
                    btn:[],
                    area: ['860px', '600px'],
                    content: '../../common/vendor_list.html?index=-1&queryId=relationId'
                });
            }
        });

        $('#query').click(function(){
            var queryParams = app.serializeForm();
            queryParams.orderType = orderType;
            queryParams.code = deletePrefix(queryParams.code);
            $("#table").datagrid("reload",queryParams);
        });

        var nowDate = app.time2str(new Date());

        var dateStart = laydate.render({
            elem: 'input[name="dateStart"]',value:nowDate.substr(0,8)+'01',max:nowDate,
            done: function(value,date){
                if(value){
                    date.month=date.month-1;
                    dateEnd.config.min = date;
                }else{
                    dateEnd.config.min=dateStart.config.min;
                }
            }
        });
        var dateEnd = laydate.render({
            elem: 'input[name="dateEnd"]',value:nowDate,min:nowDate.substr(0,8)+'01',
            done: function(value,date){
                if(value){
                    date.month=date.month-1;
                    dateStart.config.max = date;
                }else{
                    dateStart.config.max=dateEnd.config.max;
                }
            }
        });

        var type = [{"value":app.DEFAULT_ALL_VALUE,"text":"全部状态"},{"value":1,"text":"未审核"},{"value":20,"text":"未付款"},{"value":21,"text":"部分付款"},{"value":22,"text":"已付款"}];
        $('#statusId').combobox({
            width:100,
            data:type,
            name:'status'
        });

        $('#table').datagrid({
            firstLoad:true,
            url:app.BASE_API_URL+'order/page',
            queryParams:{orderType:orderType},
            columns:[
                {field:'date',title:'单据日期',width:100,fixed:true,formatter:app.timeFormatter},
                {field:'code',title:'单据编号',width:160,fixed:true,formatter:function (row) {
                        return '<a class="a" href="./buy_add.html?id='+row.id+'">CG'+row.code+'</a>';
                    }},
                {field:'code',title:'源单编号',width:160,fixed:true,formatter:function (row) {
                    if(row.orderCode){
                        return '<a dg="1" class="a">CD'+row.orderCode+"</a>";
					}else{
                        return '';
					}

                    }},
                {field:'relationName',title:'供应商',fixed:true,width:160},
                {field:'dueMoney',title:'订单金额',width:100,fixed:true,n:'money'},
                {field:'num',title:'商品数量',width:80,fixed:true,n:'number'},
                {field:'discountMoney',title:'优惠金额',width:100,fixed:true,n:'money'},
                {field:'realMoney',title:'应付金额',width:100,fixed:true,n:'money',formatter:function (row) {
                        return row.dueMoney;
                    }},
                {field:'status',title:'单据状态',width:80,fixed:true,formatter:function (row) {
                        if( row.status == '1' ){
                            return app.getLable('未审核','success');
                        }else if( row.status == '20' ){
                            return app.getLable('未付款','warning');
                        }else if( row.status == '21' ){
                            return app.getLable('部分付款','warning');
                        }else if( row.status == '22' ){
                            return app.getLable('已付款','success');
                        }else{
                            return app.getLable('状态异常','danger');
                        }
                    }},
                {field:'userName',title:'制单人',width:100,fixed:true}
            ],
            loadSuccess:loadDataSuccess
        });
    });

    function loadDataSuccess(){
        $('.a[dg="1"]').click(function(){
            var row = $(this).parent().parent().data();

            var href = "./purchase/purchase/purchase_add?code="+row.orderCode+"&id="+row.orderId+"&status="+row.orderStatus+"&orderType="+row.orderOrderType;
            top.menuClick({id:101,name:'采购订单',href:href,center:true});

        });
    }
</script>

</body>
</html>